using GameKit.UI;
using UnityEngine;

namespace GameKitUnity.UI
{
    public class UIKit : AbsUIKit<GameObject>
    {
        public static UIKit Instance { get; } = new();
        private Canvas _canvas;

        protected override IPanel MakePanel(GameObject asset)
        {
            var rootTransform = GetCanvas().transform;
            GameObject go = Object.Instantiate(asset, rootTransform, false);
            var panel = go.GetComponent<UIPanel>();
            if (!panel)
            {
                panel = go.AddComponent<UIPanel>();
            }

            return panel;
        }

        public Canvas GetCanvas()
        {
            return _canvas ?? GameObject.Find("Canvas").GetComponent<Canvas>();
        }

        public void Clear()
        {
            foreach (Transform child in GetCanvas().transform)
            {
                Object.Destroy(child.gameObject);
            }
        }
    }
}